/**
 * 库位信息相关API接口
 * 
 * 使用说明：
 * 1. 引入axios实例
 * 2. 根据实际后端接口地址修改baseURL
 * 3. 在组件中导入需要的API方法
 */

import axios from 'axios'

// 创建axios实例
const request = axios.create({
  baseURL: '/api', // 根据实际情况修改
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// 请求拦截器
request.interceptors.request.use(
  config => {
    // 可以在这里添加token等认证信息
    // const token = localStorage.getItem('token')
    // if (token) {
    //   config.headers.Authorization = `Bearer ${token}`
    // }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
request.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    return Promise.reject(error)
  }
)

// ==================== 库位信息接口 ====================

/**
 * 新增库位信息
 * @param data 库位数据
 * @returns Promise
 */
export const addStoragelocation = (data: {
  storagelocationId?: number
  storagelocationCode?: string
  storagelocationName: string
  storagelocationCapacity: string
  storagelocationState: number
  storagelocationContent: string
  storagelocationScore: number
  supervisorId: number
  dataStatus: number
  operatorName: string
}) => {
  return request({
    url: 'http://localhost:5079/api/Storagelocation/AddStoragelocation',
    method: 'post',
    data
  })
}

/**
 * 修改库位信息
 * @param data 库位数据
 * @returns Promise
 */
export const updateStoragelocation = (data: {
  storagelocationId: number
  storagelocationCode?: string
  storagelocationName: string
  storagelocationCapacity: string
  storagelocationState: number
  storagelocationContent: string
  storagelocationScore: number
  supervisorId: number
  dataStatus: number
  operatorName: string
}) => {
  return request({
    url: 'http://localhost:5079/api/Storagelocation/UpdateStoragelocation',
    method: 'put',
    data
  })
}

/**
 * 删除库位信息
 * @param storagelocationId 库位编号
 * @param operatorName 操作人名称
 * @returns Promise
 */
export const deleteStoragelocation = (storagelocationId: number, operatorName: string) => {
  return request({
    url: 'http://localhost:5079/api/Storagelocation/DeleteStoragelocation',
    method: 'get',
    params: {
      storagelocationId,
      operatorName
    }
  })
}

/**
 * 分页查询库位信息（包含主管名称）
 * @param params 查询参数
 * @returns Promise
 */
export const queryStoragelocation = (params: {
  PageIndex: number
  PageSize: number
  StoragelocationName?: string // 库位名称（模糊查询）
  StoragelocationState?: number // 库位状态（可选筛选）1.启用 2.禁用
}) => {
  return request({
    url: 'http://localhost:5110/api/Storagelocation/QueryStoragelocation',
    method: 'get',
    params
  })
}

/**
 * 获取操作人列表
 * @returns Promise
 */
export const getOperatorList = () => {
  return request({
    url: '/Users/GetOperatorList',
    method: 'get'
  })
}

/**
 * 获取主管列表
 * @returns Promise
 */
export const getSupervisorList = () => {
  return request({
    url: '/Users/GetSupervisorList',
    method: 'get'
  })
}

/**
 * 获取仓库主管列表
 * @returns Promise
 */
export const getWarehouseSupervisorList = () => {
  return request({
    url: 'http://localhost:5110/api/WarehouseSupervisor/WarehousesupervisorsQuery',
    method: 'get'
  })
}

/**
 * 获取部门级联数据
 * @param parentId 父级部门ID，默认0获取所有
 * @returns Promise
 */
export const getDeptCascaderList = (parentId: number = 0) => {
  return request({
    url: 'http://localhost:5110/api/WarehouseDept/GetDeptCascader',
    method: 'get',
    params: { parentId }
  })
}

// ==================== 仓库信息接口 ====================

/**
 * 新增仓库信息
 * @param data 仓库数据
 * @returns Promise
 */
export const addWarehouse = (data: {
  warehouseID: number
  warehouseCode: number
  warehouseName: string
  warehousecapacity: string
  supervisorId: number
  warehouseDeptId: number
  warehousePhone: string
  warehouseAddress: string
  warehouseState: number
  warehouseLockdown: boolean
  warehouseContent: string
  dataStatus: number
  operatorName: string
}) => {
  return request({
    url: 'http://localhost:5079/api/Warehouse/AddWarehouse',
    method: 'post',
    data
  })
}

/**
 * 获取仓库编号
 * @returns Promise
 */
export const getWarehouseCode = () => {
  return request({
    url: 'http://localhost:5079/api/Warehouse/GetWarehouseCode',
    method: 'get'
  })
}

/**
 * 获取操作人名称
 * @returns Promise
 */
export const getWarehouseOperatorName = () => {
  return request({
    url: 'http://localhost:5079/api/Warehouse/GetOperatorName',
    method: 'get'
  })
}

export default {
  addStoragelocation,
  updateStoragelocation,
  deleteStoragelocation,
  queryStoragelocation,
  getOperatorList,
  getSupervisorList,
  getWarehouseSupervisorList,
  getDeptCascaderList,
  addWarehouse,
  getWarehouseCode,
  getWarehouseOperatorName
}

